﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Text;
using System.Web;
using Castle.Components.Binder;
using Castle.MonoRail.Framework;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Activity;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.Services;
using Rodunu.Movies.Interfaces.Subscriptions;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Activity;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.Subscriptions;
using Rodunu.Movies.Logic.User;
using System.Linq;

namespace Rodunu.Movies.Web.Controllers
{
    public class SubscriptionsController : UserBaseController
    {
        private readonly IServices _servicesLogic;
        private readonly IUser _userLogic;
        private readonly ISubscriptions _subscriptionsLogic;
        private readonly IServiceRates _serviceRatesLogic;

        public SubscriptionsController()
        {
            _userLogic = new UserLogic();
            _subscriptionsLogic = new SubscriptionsLogic();
            _servicesLogic = new ServicesLogic();
            _serviceRatesLogic = new ServiceRatesLogic();
        }

        public void Subscribe(long id)
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);
            IServiceRates ratesLogic = new ServiceRatesLogic();
            Contracts.Services service = _servicesLogic.Find(id);
            PropertyBag["Service"] = service;
            PropertyBag["ServiceRates"] = ratesLogic.FindByBase("Services", service).OrderBy(t => t.Type).ToList();
        }

        public void CompleteSubscription([DataBind("Rate")] Contracts.ServiceRates rate, [DataBind("Service")] Contracts.Services service)
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);
            UserInfo userInfo = _userLogic.Find(User.Id);
            Contracts.Services serviceInfo = _servicesLogic.Find(service.Id);
            ServiceSubscriptions subscription = _subscriptionsLogic.GetSubscription(userInfo, serviceInfo);
            
            if (subscription == null)
            {
                if (service.Id > 0 && rate.Id > 0)
                {
                    Contracts.Services subService = _servicesLogic.Find(service.Id);
                    Contracts.ServiceRates subRates = _serviceRatesLogic.Find(rate.Id);
                    subscription = new ServiceSubscriptions
                                       {
                                           Enabled = true,
                                           DateSubscribed = DateTime.Now,
                                           DateLastRenewed = DateTime.Now,
                                           UserId = userInfo,
                                           ServiceRates = subRates,
                                           Services = subService
                                       };
                    _subscriptionsLogic.Create(subscription);
                    PropertyBag["Subscription"] = subscription;
                    PropertyBag["Rate"] = subRates;
                    PropertyBag["Service"] = subService;
                    PropertyBag["CompletionMessage"] = "Subscription Activation Successful";
                }
                else
                {
                    PropertyBag["CompletionMessage"] = "There was an error completing subscription activation.";
                }
            }
            else
            {
                PropertyBag["Subscription"] = subscription;
                PropertyBag["CompletionMessage"] = "Subscription Already Active";
                PropertyBag["Service"] = _servicesLogic.Find(service.Id);
            }
        }

        public void Unsubscribe(long Id)
        {
            SetUser();
            Contracts.Services service = _servicesLogic.Find(Id);
            UserInfo userInfo = _userLogic.Find(User.Id);
            ServiceSubscriptions subscription = _subscriptionsLogic.GetSubscription(userInfo, service);

            if (subscription != null)
            {
                if (subscription.Enabled)
                {
                    _subscriptionsLogic.Delete(subscription);
                    RenderText("You have successfully unsubscribed from our " + service.Name + " service.");
                }
                else
                {
                    RenderText("Subscriptions must be active before you can unsubscribe.  Please contact the site administrator.");
                }
            }
            else
            {
                RenderText("You are not subscribed to this service.");
            }
        }
    }
}
